package com.spic.business.train.plan.domain;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.google.common.collect.Lists;
import com.spic.common.core.annotation.Excel;
import com.spic.common.core.exception.ServiceException;
import com.spic.common.core.utils.StringUtils;
import com.spic.common.security.utils.cache.UserCacheUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@ApiModel(value = "培训班次")
@Data
public class TrainPlanShift {
    /**
     *
     */
    @ApiModelProperty(value = "主键")
    private String id;

    /**
     * 父id
     */
    @ApiModelProperty(value = "父id")
    private String trainPlanId;

    @ApiModelProperty(value = "班次名")
    @Excel(name="班名")
    private String className;
    /**
     * 计划开始时间
     */
    @ApiModelProperty(value = "计划开始时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    @Excel(name="计划开始时间",dateFormat = "yyyy-MM-dd")
    private Date starTime;

    /**
     * 计划结束时间
     */
    @ApiModelProperty(value = "计划结束时间")
    @Excel(name="计划结束时间",dateFormat = "yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    private Date endTime;

    @ApiModelProperty(value = "班主任")
    @Excel(name="班主任")
    private String classDirector;
    /**
     * 班/天
     */
    @ApiModelProperty(value = "班/天")
    @Excel(name="班/天")
    private BigDecimal trainDay;

    /**
     * 课时/天
     */
    @ApiModelProperty(value = "课时/天")
    @Excel(name="课时/天")
    private BigDecimal schoolHour;

    /**
     * 学习人员id列表
     */
    @ApiModelProperty(value = "学习人员id列表")
    @NotBlank(message = "学习人员不能为空")
    private String learnerIds;

    @ApiModelProperty(value = "学习人员姓名")
    private List<String> learnerName;



    @ApiModelProperty(value = "培训部门")
    private String dept;



    public void transCode(){
        if(StringUtils.isEmpty(learnerIds)) return;
        String[] ids = this.learnerIds.split(",");
        learnerName = Lists.newArrayListWithCapacity(ids.length);
        for (String id : ids) {
            try {
                learnerName.add(UserCacheUtils.getValue(Long.parseLong(id)));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void checkParam(){
        if(StringUtils.isEmpty(this.learnerIds))
            throw new ServiceException("学习人员不能为空");
        if(StringUtils.isEmpty(this.classDirector))
            throw new ServiceException("班主任不能为空");
        if(StringUtils.isEmpty(this.dept))
            throw new ServiceException("培训部门不能为空");
        if(trainDay == null)
            throw new ServiceException("'班/天'不能为空");
        if(schoolHour == null)
            throw new ServiceException("'课时/天' 不能为空");
    }
}

